package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetDish;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    UserService userService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long currentThread = BaseContext.getCurrentThread();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<ShoppingCart>().eq("user_id", currentThread);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        Long id = BaseContext.getCurrentThread();
        shoppingCart.setUserId(id);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<ShoppingCart>().eq("user_id", id);
        queryWrapper.eq(dishId != null, "dish_id", dishId);
        queryWrapper.eq(setmealId != null, "setmeal_id", setmealId);
        ShoppingCart  byId = shoppingCartService.getOne(queryWrapper);

        if (byId != null) {
            byId.setNumber(byId.getNumber() + 1);
            shoppingCartService.updateById(byId);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(id);
            shoppingCartService.save(shoppingCart);
            byId = shoppingCart;
        }
        return R.success(byId);
    }

    @PostMapping("/sub")
    public R sub(@RequestBody SetDish setDish) {
        Long id = BaseContext.getCurrentThread();
        Long dishId = setDish.getDishId();
        Long setmealId = setDish.getSetmealId();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<ShoppingCart>().eq("user_id", id);
        queryWrapper.eq(dishId != null, "dish_id", dishId);
        queryWrapper.eq(setmealId != null, "setmeal_id", setmealId);
        ShoppingCart  byId = shoppingCartService.getOne(queryWrapper);
        if (byId == null) {
            throw new CustomException("操作有误！当前菜品没在购物车");
        }

        if (byId.getNumber() == 1) {
            shoppingCartService.removeById(byId.getId());
            byId.setNumber(0);
        } else {
            byId.setNumber(byId.getNumber() - 1);
            shoppingCartService.updateById(byId);
        }
        return R.success(byId);
    }

    @DeleteMapping("/clean")
    public R clean(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Long id = user.getId();
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空");
    }
}
